package com.hxzy.shop.commodity.controller;

import com.github.pagehelper.PageInfo;
import com.hxzy.shop.commodity.dto.req.*;
import com.hxzy.shop.common.dto.commodity.req.ClassificationsReq;
import com.hxzy.shop.common.dto.commodity.resp.ClassifiedManagementResp;
import com.hxzy.shop.commodity.service.ClassifiedManagementService;
import com.hxzy.shop.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/6/30 10:05
 * @Version 1.0
 */
@RestController
@Api(tags = "分类管理")
@RequestMapping("/classification")
public class ClassificationsController {

    @Autowired
    private ClassifiedManagementService classifiedManagementService;



    /**
     * 获得分类列表分页信息
     *
     * @param req 分页封装请求类
     * @return 返回分页列表
     */
    @ApiOperation("获得分类管理分页")
    @GetMapping("/getClassifiedPage")
    public Result<PageInfo<ClassifiedManagementResp>> getClassifiedPage(ClassificationsReq req) {

        PageInfo<ClassifiedManagementResp> result = classifiedManagementService.getClassifiedPage(req);

        return Result.success(result);
    }

    /********************新***********************/
    @ApiOperation("获得分类列表")
    @PostMapping("/getClassifiedManagementList")
    public Result<List<ClassifiedManagementResp>> getClassifiedManagementList(){
        System.out.println("------getClassifiedManagementList--------");
        return Result.success(classifiedManagementService.getClassifiedManagement());
    }
    @ApiOperation("新增分类")
    @PostMapping("/addNewClassified")
    public Result<Object> addNewClassified(@RequestBody AddNewClassifiedReq req){

        return Result.success(classifiedManagementService.addNewClassified(req));
    }
    @ApiOperation("编辑分类")
    @PostMapping("/editClassified")
    public Result<Object> editClassified(@RequestBody EditClassifiedReq req){
        return Result.success(classifiedManagementService.editClassified(req));
    }

    @ApiOperation("删除分类")
    @PostMapping("/deleteClassifiedById")
    public Result<Object> deleteClassifiedById(@RequestBody ClassifiedIdReq req){
        return Result.success(classifiedManagementService.deleteClassifiedById(req));
    }

    @ApiOperation("调整分类排序")
    @PostMapping("/changeClassificationSort")
    public Result<Object> changeClassificationSort(@RequestBody ClassificationSortReq req){
        return Result.success(classifiedManagementService.changeClassificationSort(req));
    }



    /**
     * 获得分类列表
     *
     * @return 分类列表
     */
    @ApiOperation("获得分类列表")
    @PostMapping("/getClassifiedList")
    public Result<List<ClassifiedManagementResp>> getClassifiedList() {
        List<ClassifiedManagementResp> result = classifiedManagementService.getClassifiedList();
        return Result.success(result);
    }

    /**
     * 添加分类
     *
     * @param addClassifiedReq 添加分类请求类
     * @return 是否添加成功
     */
    @ApiOperation("添加分类")
    @PostMapping("/addClassified")
    public Result<Boolean> addClassified(@ApiParam(required = true, name = "addClassifiedVo", value = "添加分类的model")
                                         @RequestBody @Valid AddClassifiedReq addClassifiedReq) {
        boolean result = classifiedManagementService.addClassified(addClassifiedReq);

        return Result.success(result);
    }
    /**
     * 根据id 获得分类详细信息
     *
     * @param id 分类id
     * @return 返回分类信息
     */
    @PostMapping("/getClassified/{id}")
    @ApiOperation("根据id获得分类详细信息")
    public Result<ClassifiedManagementResp> getClassified(@ApiParam(required = true, name = "id", value = "分类的id")
                                                          @PathVariable("id") Long id) {

        ClassifiedManagementResp result = classifiedManagementService.getClassified(id);

        return Result.success(result);
    }
    /**
     * 更新分类
     *
     * @param addClassifiedReq 更新分类实体类
     * @return 是否更新成功
     */
    @ApiOperation("更新分类")
    @PostMapping("/updateClassified")
    public Result<Boolean> updateClassified(@ApiParam(required = true, name = "addClassifiedVo", value = "更新分类请求model")
                                            @RequestBody AddClassifiedReq addClassifiedReq) {
        boolean result = classifiedManagementService.addClassified(addClassifiedReq);
        return Result.success(result);

    }
    /**
     * 根据id 删除指定分类
     *
     * @param id 需要删除分类的id
     * @return 是否删除成功
     */
    @PostMapping("/deleteClassified/{id}")
    @ApiOperation("根据id 删除指定的分类")
    public Result<Boolean> deleteClassified(@ApiParam(required = true, name = "id", value = "需要删除分类的id")
                                            @PathVariable("id") Long id) {

        boolean result = classifiedManagementService.deleteClassified(id);

        return Result.success(result);
    }
    /**
     * 更新状态
     *
     * @param state 状态
     * @return 是否成功
     */
    @ApiOperation("更新状态")
    @PostMapping("/updateState")
    public Result<Boolean> updateState(@ApiParam(readOnly = true, name = "state", value = "状态")
                                       @RequestBody StateUpdateRep state) {
        boolean updateResult = classifiedManagementService.updateState(state.getId(), state.getState());
        return Result.success(updateResult);
    }
}
